package com.xiaoyu.util2;

import java.util.Random;

public class RandomUtil {
	public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String LETTERCHAR = "abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	public static final String NUMBERCHAR = "0123456789";

	public static String number(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; ++i) {
			sb.append("0123456789".charAt(random.nextInt("0123456789".length())));
		}
		return sb.toString();
	}

	public static String String(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; ++i) {
			sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(random
					.nextInt("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
							.length())));
		}
		return sb.toString();
	}

	public static String MixString(int length) {
		StringBuffer sb = new StringBuffer();
		Random random = new Random();
		for (int i = 0; i < length; ++i) {
			sb.append("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".charAt(random
					.nextInt("abcdefghijkllmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
							.length())));
		}
		return sb.toString();
	}

	public static String LowerString(int length) {
		return MixString(length).toLowerCase();
	}

	public static String UpperString(int length) {
		return MixString(length).toUpperCase();
	}

	public static String ZeroString(int length) {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < length; ++i) {
			sb.append('0');
		}
		return sb.toString();
	}

	public static String toFixdLengthString(long num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0)
			sb.append(ZeroString(fixdlenth - strNum.length()));
		else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
		}
		sb.append(strNum);
		return sb.toString();
	}

	public static String toFixdLengthString(int num, int fixdlenth) {
		StringBuffer sb = new StringBuffer();
		String strNum = String.valueOf(num);
		if (fixdlenth - strNum.length() >= 0)
			sb.append(ZeroString(fixdlenth - strNum.length()));
		else {
			throw new RuntimeException("将数字" + num + "转化为长度为" + fixdlenth + "的字符串发生异常！");
		}
		sb.append(strNum);
		return sb.toString();
	}

	public static int getNotSimple(int[] param, int len) {
		Random rand = new Random();
		for (int i = param.length; i > 1; --i) {
			int index = rand.nextInt(i);
			int tmp = param[index];
			param[index] = param[(i - 1)];
			param[(i - 1)] = tmp;
		}
		int result = 0;
		for (int i = 0; i < len; ++i) {
			result = result * 10 + param[i];
		}
		return result;
	}
}